package datastruct.tree;

/**
 * 线段树（待深入学习）
 * RMQ经典问题
 * @author RunningShrimp
 * @date 2021/5/26  20:53
 */
@SuppressWarnings("unchecked")
public class SegmentTree<E> {
    private final E[] data;
    private final E[] tree;

    public SegmentTree(E[] array) {
        data = (E[]) new Object[array.length];
        System.arraycopy(array, 0, data, 0, array.length);
        tree = (E[]) new Object[4 * array.length];
        buildSegmentTree(0, 0, data.length - 1);
    }

    /**
     * 在treeIndex 的位置创建表示区间线段树
     *
     * @param treeIndex
     * @param l
     * @param r
     */
    private void buildSegmentTree(int treeIndex, int l, int r) {
        if (l == r) {
            tree[treeIndex] = data[l];
        }
        int leftTreeIndex = leftChild(treeIndex);
        int rightTreeIndex = rightChild(treeIndex);
        int mid = l + (r - l) / 2;

        buildSegmentTree(leftTreeIndex, l, mid);
        buildSegmentTree(rightTreeIndex, mid + 1, r);

        //TODO：执行相应的操作
//        tree[treeIndex] = tree[leftTreeIndex]+tree[rightTreeIndex];

    }

    public int getSize() {
        return data.length;
    }

    public E get(int index) {
        if (index < 0 || index > data.length) {
            throw new IndexOutOfBoundsException("索引无效");
        }
        return data[index];
    }

    /**
     * 返回完全二叉树的数组表示中，一个索引的所表示的元素的左孩子节点的索引
     *
     * @param index
     * @return
     */
    private int leftChild(int index) {
        return 2 * index + 1;
    }

    /**
     * 返回完全二叉树的数组表示中，一个索引的所表示的元素的右孩子节点的索引
     *
     * @param index
     * @return
     */
    private int rightChild(int index) {
        return 2 * index + 2;
    }

    public E query(int queryL, int queryR) {
        if (queryL < 0 || queryL > data.length || queryR < 0 || queryR >= data.length || queryL > queryR) {
            throw new IndexOutOfBoundsException("索引无效");
        }
        return query(0, 0, data.length - 1, queryL, queryR);
    }

    /**
     * 在treeIndex为根的线段树[l,r]的范围里，搜索区间[queryL,queryR]的值
     *
     * @param treeIndex
     * @param l
     * @param r
     * @param queryL
     * @param queryR
     * @return
     */
    private E query(int treeIndex, int l, int r, int queryL, int queryR) {
        if (l == queryL && r == queryR) {
            return tree[treeIndex];
        }
        int mid = l + (r - l) / 2;
        int leftTreeIndex = leftChild(treeIndex);
        int rightTreeIndex = rightChild(treeIndex);

        if (queryL >= mid + 1) {
            return query(rightTreeIndex, mid + 1, r, queryL, queryR);
        } else if (queryR <= mid) {
            return query(leftTreeIndex, l, mid, queryL, queryR);
        }
        E leftResult = query(leftTreeIndex, l, mid, queryL, mid);
        E rightResult = query(rightTreeIndex, mid + 1, r, mid + 1, queryR);
        //TODO: 进行业务操作
//        return leftResult + rightResult;
        return null;
    }

    public void set(int index, E value) {
        if (index < 0 || index > data.length) {
            throw new IndexOutOfBoundsException("索引无效");
        }
        data[index] = value;
        set(0, 0, data.length - 1, index, value);
    }

    private void set(int treeIndex, int l, int r, int index, E e) {
        if (l == r) {
            tree[treeIndex] = e;
            return;
        }
        int mid = l + (r - l) / 2;
        int leftTreeIndex = leftChild(treeIndex);
        int rightTreeIndex = rightChild(treeIndex);

        if (index >= mid + 1) {
            set(rightTreeIndex, mid + 1, r, index, e);
        } else {
            set(leftTreeIndex, l, mid, index, e);
        }

        //更新父节点
//        tree[treeIndex] = tree[leftTreeIndex]+tree[rightTreeIndex];
    }
}
